package cn.stylefeng.guns.modular.robot.cmd.impl;

import cn.stylefeng.guns.modular.robot.cmd.abs.AbstractCmd;
import cn.stylefeng.guns.modular.robot.entity.Group;
import cn.stylefeng.guns.modular.robot.entity.enums.GroupMode;
import cn.stylefeng.guns.modular.robot.service.GroupService;
import cn.stylefeng.guns.modular.robot.utils.RobotUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.objects.Update;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Objects;

@Slf4j
@Service
@Transactional
public class CmdExchangeRate extends AbstractCmd {

    @Resource
    private GroupService groupService;

    @Override
    public void exec(Update update, TelegramLongPollingBot bot) {
        String text = update.getMessage().getText();
        Long chatId = update.getMessage().getChatId();
        Group group = groupService.getGroupByTgGroupId(chatId);
        if (Objects.isNull(group)) {
            return;
        }
        group.setExchangeRate(new BigDecimal(text.replace("设置汇率", "").trim()));
        if (Boolean.TRUE.equals(group.getBatchStatus()) && group.getExchangeRate().compareTo(BigDecimal.valueOf(0)) == 0 && GroupMode.USDT.equals(group.getGroupMode())) {
            RobotUtils.sendText(chatId, "在U本位模式下汇率不能够为0\n请结束账单再进行使用\n", bot);
            return;
        }
        if (Boolean.TRUE.equals(group.getBatchStatus()) &&  GroupMode.币.equals(group.getGroupMode())) {
            RobotUtils.sendText(chatId, "在币本位模式下不能修改汇率\n请结束账单再进行使用\n", bot);
            return;
        }

        if (group.getExchangeRate().compareTo(BigDecimal.valueOf(0)) == 0) {
            group.setGroupMode(GroupMode.币);
        } else {
            group.setGroupMode(GroupMode.USDT);
        }
        groupService.updateById(group);
        RobotUtils.sendText(chatId, "本群汇率已修改为：" + group.getExchangeRate(), bot);
    }

    @Override
    public String getCmdName() {
        return "设置汇率";
    }

    @Override
    public String getDescribe() {
        return "设置汇率                    设置群汇率，如：设置汇率1\n";
    }
}
